package com.central.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.central.modules.sys.dao.SysDictDataDao;
import com.central.modules.sys.entity.SysDictDataEntity;
import com.central.modules.sys.model.AddSysDictDataRequest;
import com.central.modules.sys.model.SysDictDataQueryBean;
import com.central.modules.sys.service.SysDictDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataDao, SysDictDataEntity> implements SysDictDataService {
    @Override
    public IPage<SysDictDataEntity> searchSysDictDatas(SysDictDataQueryBean queryBean) {
        Page<SysDictDataEntity> page = new Page<>(queryBean.getPageStart(), queryBean.getPageSize());
        LambdaQueryWrapper<SysDictDataEntity> queryWrapper = new LambdaQueryWrapper<SysDictDataEntity>();
        queryWrapper.orderByAsc(SysDictDataEntity::getDictSort);
        if (Objects.nonNull(queryBean.getDictTypeId())) {
            queryWrapper.eq(SysDictDataEntity::getDictTypeId, queryBean.getDictTypeId());
        }
        return this.page(page, queryWrapper);
    }

    @Override
    public boolean addSysDictData(AddSysDictDataRequest request) {
        SysDictDataEntity sysDictData = new SysDictDataEntity();
        BeanUtils.copyProperties(request, sysDictData);
        sysDictData.setStatus(1);
        return this.save(sysDictData);
    }

    @Override
    public boolean updateSysDictData(Long id, AddSysDictDataRequest request) {
        SysDictDataEntity sysDictData = new SysDictDataEntity();
        BeanUtils.copyProperties(request, sysDictData);
        sysDictData.setStatus(1);
        sysDictData.setId(id);
        return this.updateById(sysDictData);
    }

    @Override
    public boolean removeSysDictData(Long id) {
        return this.removeById(id);
    }

    @Override
    public boolean cleanSysDictDataByTypeId(Long dictTypeId) {
        LambdaQueryWrapper<SysDictDataEntity> queryWrapper = new LambdaQueryWrapper<SysDictDataEntity>();
        queryWrapper.eq(SysDictDataEntity::getDictTypeId, dictTypeId);
        return this.remove(queryWrapper);
    }

    @Override
    public List<SysDictDataEntity> findAllByTypeId(Long dictTypeId) {
        LambdaQueryWrapper<SysDictDataEntity> queryWrapper = new LambdaQueryWrapper<SysDictDataEntity>();
        queryWrapper.orderByAsc(SysDictDataEntity::getDictSort);
        queryWrapper.eq(SysDictDataEntity::getDictTypeId, dictTypeId);
        queryWrapper.eq(SysDictDataEntity::getStatus, 1);
        return this.list(queryWrapper);
    }
}
